# coding=utf-8
import tensorflow as tf
import numpy as np

print("安装版本号：" + tf.__version__)
# 使用NumPy 生成假数据(phony data), 总共100个点
x_data = np.float32(np.random.rand(2, 100))
y_data = np.dot([0.100, 0.200], x_data) + 0.300
# 构造一个线性模型
b = tf.Variable(tf.zeros([1]))
w = tf.Variable(tf.random.uniform([1, 2], -1.0, 1.0))
y = tf.matmul(w, x_data) + b

# 最小化方差
loss = tf.reduce_mean(tf.square(y - y_data))
optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5).minimize(loss)
train = optimizer.minimize(loss)

with tf.Session() as sess:
  sess.run(tf.global_variables_initializer())

  # 进行一些训练代码，此处省略
  # xxxxxxxxxxxx

  # 显示图中的节点
  print([n.name for n in sess.graph.as_graph_def().node])
  frozen_graph_def = tf.graph_util.convert_variables_to_constants(
      sess,
      sess.graph_def,
      output_node_names=["z"])

  # 保存图为pb文件
  with open('model.pb', 'wb') as f:
    f.write(frozen_graph_def.SerializeToString())
